﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Text.RegularExpressions;

namespace Models
{
    public class News
    {
        [Key]
        public int Id { get; set; }

        [Required(ErrorMessage = "Required field Name")]
        [Display(Name = "Name")]
        [MinLength(3)]
        [MaxLength(50)]
        public string Title { set; get; }

        [Required(ErrorMessage = "Required field Category")]
        [Display(Name = "Category")]
        public int Category { set; get; }

        [Required(ErrorMessage = "Required field Author")]
        [Display(Name = "Author")]
        [MaxLength(50)]
        public string Author { get; set; }

        [Required(ErrorMessage = "Required field Text")]
        [Display(Name = "Text")]
        [DataType(DataType.MultilineText)]
        public string Text { set; get; }

        [Display(Name = "Rating")]
        public decimal Rating { set; get; }

        [Display(Name = "Votes")]
        public int Votes { set; get; }

        [Display(Name = "Date of publish date")]
        public DateTime? PublishDate { set; get; }

        [Display(Name = "Date of last update")]
        public DateTime? UpdateDate { set; get; }

        public List<Tag> TagsList { set; get; }

        public List<ImageInNews> Images { set; get; } 

    }

    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
    public class EmailAddressAttribute : DataTypeAttribute
    {
        private readonly Regex regex = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", RegexOptions.Compiled);

        public EmailAddressAttribute()
            : base(DataType.EmailAddress)
        {

        }

        public override bool IsValid(object value)
        {

            string str = Convert.ToString(value, CultureInfo.CurrentCulture);
            if (string.IsNullOrEmpty(str))
                return true;

            Match match = regex.Match(str);
            return ((match.Success && (match.Index == 0)) && (match.Length == str.Length));
        }
    }
}